﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json.Serialization;
using Sustainalytics.Utils;
using Sustainalytics.Utils.WebApi.Filters;

namespace Sustainalytics.Services.Admin.WebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // action filter for setting cacheability headers
            config.Filters.Add(new CacheabilityActionFilterAttribute());

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Filters.Add(new ExceptionHandlingAttribute());
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Routes.MapHttpRoute(
                name: "ApiWithAction",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            //config.Routes.MapHttpRoute(
            //    name: "status",
            //    routeTemplate: "api/{controller}/status/{status}"
            //    );
            //config.Routes.MapHttpRoute(
            //    name: "search",
            //    routeTemplate: "api/{controller}/search/{search}"
            //    );

        }
    }
}
